import { useQuery } from '@tanstack/vue-query'
import { type MaybeRef, computed, isRef } from 'vue'
import type { GetUploadURLKey } from './types'
import { ApiGetUploadURL } from '.'

export function useGetUploadURLQueryKey(
  query: MaybeRef<GetUploadURLKey[2]>
): MaybeRef<GetUploadURLKey> {
  return isRef(query) ? computed(() => ['get', 'upload', query.value]) : ['get', 'upload', query]
}
export function useGetUploadURL(query: MaybeRef<GetUploadURLKey[2]>) {
  return useQuery({
    staleTime: 240_000,
    queryKey: useGetUploadURLQueryKey(query),
    queryFn: ({ queryKey: [, , query] }) => ApiGetUploadURL(query),
    select: ({ data }) => data
  })
}
